package com.wh.wisdomsite.xbox.server.controller.vi;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.vi.CompHkUserIn;
import com.wh.wisdomsite.xbox.us.entity.Company;
import com.wh.wisdomsite.xbox.us.entity.HkUser;
import com.wh.wisdomsite.xbox.us.service.ICompanyService;
import com.wh.wisdomsite.xbox.us.service.IHkUserService;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Org;
import com.wh.wisdomsite.xbox.vi.entity.CompHkUser;
import com.wh.wisdomsite.xbox.vi.service.ICompHkUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司下海康用户controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.vi
 * @date 2018年09月05日 16:03
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/compHkUser")
public class CompHkUserController {
    private static final Log logger = LogFactory.getLog(VideoDevDataController.class);

    @Autowired
    private ICompHkUserService iCompHkUserService;

    @Autowired
    private IHkUserService iHkUserService;

    @Autowired
    private ICompanyService iCompanyService;


    /**
     * 跳转到海康用户配置
     * @return
     */
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index() {
        return "vi/compHkUser/compHkUserList";
    }


    /**
     * 获取海康用户配置列表
     * @param response
     * @param in
     */
    @RequestMapping(value = "/compHkUserList.do")
    public void compHkUserList(HttpServletResponse response, CompHkUserIn in) {
        try {
            logger.info("加载海康用户配置列表-->>开始");
            String compId = in.getCompanyId();
            String compCode = iCompanyService.selectById(compId).getCompCode();
            //设置条件
            EntityWrapper<CompHkUser> ewCompHkUser = new EntityWrapper<CompHkUser>();
            ewCompHkUser.setEntity(new CompHkUser());
            if (StringUtils.isNotEmpty(in.getCompanyId())) {
                ewCompHkUser.like("v.companyCode", compCode);
            }
            List<Map<String, Object>> compHkUSer = iCompHkUserService.getMaps(ewCompHkUser);
            JSONArray array = new JSONArray();
            if (compHkUSer.size() > 0) {
                for (Map<String, Object> compHkUser : compHkUSer) {
                    JSONObject object = new JSONObject(compHkUser);
                    if (compHkUser.get("company") != null) {
                        object.put("company", new JSONObject(compHkUser.get("company")));
                    }
                    array.put(object);
                }
            }
            ResManager.getJsonOut(response).print(array);
            logger.info("加载海康用户配置列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除人员配置
     * @param response
     * @return
     */
    @RequestMapping(value = "/delCompHkUser.do")
    public void delCompHkUser(String chkId, HttpServletResponse response) {
        try{
            logger.info("删除人员配置-->>开始");
            if(StringUtils.isNotEmpty(chkId)){
                String chkIds = chkId.substring(0,chkId.length()-1);
                String[] chkIdArr = chkIds.split(",");
                List<String> chkIdList = new ArrayList<String>();
                for(String s : chkIdArr){
                    chkIdList.add(s);
                }
                iCompHkUserService.deleteBatchIds(chkIdList);
                logger.info("删除人员配置-->>结束");
            }
            ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("删除成功"));
        }catch (Exception e){
            e.printStackTrace();
            ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("删除失败"));
        }

    }

    /**
     * 返回未配置的用户
     * @param response
     */
    @RequestMapping("/getNotAllotUser.do")
    public void getNotAllotUser(HttpServletResponse response){
        try{
            logger.info("返回未配置的用户-->>开始");
            List<HkUser> hkUserList = iHkUserService.getNotAllotUser();
            JSONArray jsonArray = new JSONArray();
            if(hkUserList.size() > 0){
                for(HkUser hkUser : hkUserList){
                    JSONObject object = new JSONObject(hkUser);
                    jsonArray.put(object);
                }
            }
            ResManager.getJsonOut(response).print(jsonArray);
            logger.info("返回未配置的用户-->>结束");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 保存分配的用户
     * @param response
     * @param in
     */
    @RequestMapping("/save.do")
    public void save(HttpServletResponse response,CompHkUserIn in){
        try{
            logger.info("保存配置的用户-->>开始");
            String companyId = in.getCompanyId();
            String userUuids = in.getUserUuid();
            if(StringUtils.isNotEmpty(companyId) && StringUtils.isNotEmpty(userUuids)){
                String userUuids1 = userUuids.substring(0,userUuids.length()-1);
                Company company = iCompanyService.selectById(companyId);
                String compCode = company.getCompCode();
                List<HkUser> hkUserList = iHkUserService.getNotAllotUser();
                Map<String,String> hkUserMap = new HashMap<String,String>();
                for(HkUser hkUser : hkUserList){
                    hkUserMap.put(hkUser.getUserUuid(),hkUser.getLoginAccount());
                }
                String[] userUuidArr = userUuids1.split(",");
                for(String s : userUuidArr){
                    CompHkUser compHkUser = new CompHkUser();
                    compHkUser.setCompanyId(Integer.valueOf(companyId));
                    compHkUser.setCompanyCode(compCode);
                    compHkUser.setUserUuid(s);
                    compHkUser.setLoginAccount(hkUserMap.get(s));
                    iCompHkUserService.insert(compHkUser);
                }
            }
            response.getWriter().write(JsonUtil_Org.returnTrue("保存成功"));

            logger.info("保存配置的用户-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("保存配置的用户-->>Exception" + e.getMessage());
        }
    }








}
